package top.yangbuyi.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.Duration;
import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import sun.rmi.runtime.Log;
import top.yangbuyi.constants.Constants;
import top.yangbuyi.domain.DictType;
import top.yangbuyi.dto.DictDataDto;
import top.yangbuyi.mapper.DictDataMapper;
import top.yangbuyi.domain.DictData;
import top.yangbuyi.mapper.DictTypeMapper;
import top.yangbuyi.service.impl.DictDataService;
import top.yangbuyi.vo.DataGridView;

/**
 * ClassName: Loginfo
 * Description: 杨不易网站 :www.yangbuyi.top
 * date: 2020/8/29
 *
 * @author TeouBle
 * @author yangbuyi
 * @since JDK 1.8
 **/

@Service
@Slf4j
public class DictDataServiceImpl implements DictDataService {

	  @Autowired
	  private StringRedisTemplate redisTemplate;

	  @Autowired
	  private DictDataMapper dictDataMapper;

	  /**
	   * 分页查询字典数据类型
	   *
	   * @param dictDataDto
	   * @return
	   */
	  @Override
	  public DataGridView listPage(DictDataDto dictDataDto) {
			Page<DictData> dictDataPage = new Page<>(dictDataDto.getPageNum(), dictDataDto.getPageSize());
			QueryWrapper<DictData> qw = new QueryWrapper<>();
			qw.eq(StringUtils.isNotBlank(dictDataDto.getDictType()), DictData.COL_DICT_TYPE, dictDataDto.getDictType());
			qw.like(StringUtils.isNotBlank(dictDataDto.getDictLabel()), DictData.COL_DICT_LABEL, dictDataDto.getDictLabel());
			qw.eq(StringUtils.isNotBlank(dictDataDto.getStatus()), DictData.COL_STATUS, dictDataDto.getStatus());
			this.dictDataMapper.selectPage(dictDataPage, qw);
			return new DataGridView(dictDataPage.getTotal(), dictDataPage.getRecords());
	  }

	  /**
	   * 插入新的字典类型
	   *
	   * @param dictDataDto
	   * @return
	   */
	  @Override
	  public int insert(DictDataDto dictDataDto) {
			DictData dictData = new DictData();
			BeanUtil.copyProperties(dictDataDto, dictData);
			//设置创建者，创建时间
			dictData.setCreateBy(dictDataDto.getSimpleUser().getUserName());
			dictData.setCreateTime(DateUtil.date());
			return this.dictDataMapper.insert(dictData);

	  }

	  /**
	   * 修改的字典类型
	   *
	   * @param dictDataDto
	   * @return
	   */
	  @Override
	  public int update(DictDataDto dictDataDto) {
			DictData dictData = new DictData();
			BeanUtil.copyProperties(dictDataDto, dictData);
			//设置修改人
			dictData.setUpdateBy(dictDataDto.getSimpleUser().getUserName());
			return this.dictDataMapper.updateById(dictData);

	  }

	  /**
	   * 根据ID删除字典类型
	   *
	   * @param dictCodeIds
	   * @return
	   */
	  @Override
	  public int deleteDictDataByIds(Long[] dictCodeIds) {
			List<Long> ids = Arrays.asList(dictCodeIds);
			if (null != ids && ids.size() > 0) {
				  return this.dictDataMapper.deleteBatchIds(ids);
			} else {
				  return -1;
			}
	  }

	  /**
	   * 根据字典类型查询字典数据
	   *
	   * @param dictType
	   * @return
	   */
	  @Override
	  public List<DictData> selectDictDataByDictType(String dictType) {
			String key = Constants.DICT_REDIS_PROFIX + dictType;
			ValueOperations<String, String> opsForValue = redisTemplate.opsForValue();
			String json = opsForValue.get(key);
			List<DictData> dictDatas = JSON.parseArray(json, DictData.class);
			if (null == dictDatas) {
				  // redis当中没有缓存数据，则从数据库查询数据 写到redis
				  QueryWrapper<DictData> dictDataQueryWrapper = new QueryWrapper<>();
				  dictDataQueryWrapper.eq(DictData.COL_DICT_TYPE, dictType);
				  List<DictData> dictData = this.dictDataMapper.selectList(dictDataQueryWrapper);
				  // 转换为json
				  String dictData_type = JSON.toJSONString(dictData);
				  ValueOperations<String, String> value = redisTemplate.opsForValue();
				  value.set(Constants.DICT_REDIS_PROFIX + dictType, dictData_type);
				  log.info("从数据库当中查询并且将字典数据存放到redis当中:{}");
			}
			log.info("根据类型字典查询字典数据成功:{}");
			return dictDatas;

	  }

	  /**
	   * 根据ID查询一个字典类型
	   *
	   * @param dictCode
	   * @return
	   */
	  @Override
	  public DictData selectDictDataById(Long dictCode) {
			return this.dictDataMapper.selectById(dictCode);
	  }
}
